﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Com.CodePlex.SPC3.SPbAM.Data;
using Com.CodePlex.SPC3.SPbAM.Controller;
using Com.CodePlex.SPC3.SPbAM.Model;
using System.Windows.Input;
//using Com.CodePlex.SPC3.SPbAM.View; //should not reference this http://social.msdn.microsoft.com/Forums/is/wpf/thread/3bced3ea-4a97-4456-bfe5-828289bcf971

namespace Com.CodePlex.SPC3.SPbAM.ViewModel {

	public class ServerGroupViewModel : ViewModel {

		#region Init
        public ServerWorkspaceViewModel Workspace { get; private set; }
		private ServerGroupViewModel parent;
		private ServerGroup servergroup;

		public ServerGroupViewModel(ServerGroup servergroup, ServerWorkspaceViewModel workspace)
			: this(servergroup, null, workspace) {
				
		}

        public ServerGroupViewModel(ServerGroup servergroup, ServerGroupViewModel parent, ServerWorkspaceViewModel workspace) {
			if (servergroup == null) {
				throw new ArgumentNullException("servergroup");
			}
			this.servergroup = servergroup;
            this.parent = parent;
            Workspace = workspace;
            DisplayName = servergroup.Name;
			Mediator.Register(this);
			//note: lazy load: dont do more stuff here
		}
		#endregion

		#region Commands

		private ICommand newservergroupcommand;
		public ICommand NewServerGroupCommand { get { return newservergroupcommand ?? (newservergroupcommand = new RelayCommand(param => NewServerGroup())); } }

		private ICommand newservercommand;
		public ICommand NewServerCommand { get { return newservercommand ?? (newservercommand = new RelayCommand(param => NewServer())); } }

		private ICommand editcommand;
		public ICommand EditCommand { get { return editcommand ?? (editcommand = new RelayCommand(param => Edit())); } }

		private IEnumerable<CommandViewModel> treecommands;
		public IEnumerable<CommandViewModel> TreeCommands {
			get {
				if (treecommands == null) {
					treecommands = new List<CommandViewModel>() {
						new CommandViewModel("Edit", EditCommand),
						new CommandViewModel("New Server Group", NewServerGroupCommand),
						new CommandViewModel("New Server", NewServerCommand)
					};
				}
				return treecommands;
			}
		}

		#endregion

		#region Display Properties

		private bool isselected;
		public bool IsSelected {
			get { return isselected; }
			set {
				if (value != isselected) {
					isselected = value;
					OnPropertyChanged(() => IsSelected);
				}
			}
		}

		private bool isexpanded = true;
		public bool IsExpanded {
			get { return isexpanded; }
			set {
				if (value != isexpanded) {
					isexpanded = value;
					OnPropertyChanged(() => IsExpanded);
				}
				if (isexpanded && parent != null) {
					parent.IsExpanded = true;
				}
			}
		}

		private ObservableCollection<ViewModel> children;
		public ObservableCollection<ViewModel> Children {
			get {
				if (children == null) {
					children = new ObservableCollection<ViewModel>();
					children.CollectionChanged += children_CollectionChanged;
				}
				return children;
			}
		}

		private void children_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {

		}

		public string ImageSource {
			get { return "pack://application:,,,/Resources/Images/ServerGroup.jpg"; }
		}

		#endregion

		public void DataBind() {
			Children.Clear();
			(
				from child in servergroup.Children
				where child is ServerGroup
                select (ViewModel)(new ServerGroupViewModel((ServerGroup)child, Workspace))
			).Union(
				from child in servergroup.Children
				where child is Server
                select (ViewModel)(new ServerViewModel((Server)child, Workspace))
			).ToList<ViewModel>().ForEach(
				i => {
					if (i is ServerGroupViewModel) {
						((ServerGroupViewModel)i).DataBind();
					}
					Children.Add(i);
				}
			);
		}

		private void NewServerGroup() {
			ServerGroup servergroup = new ServerGroup() { Name = "New" };
			this.servergroup.Children.Add(servergroup);
            ServerGroupViewModel model = new ServerGroupViewModel(servergroup, Workspace);
			Children.Add(model);
		}

		private void NewServer() {
			Server server = new Server() { Name = "New" };
			this.servergroup.Children.Add(server);
            ServerViewModel model = new ServerViewModel(server, Workspace);
			Children.Add(model);
		}

		public void Edit() {
			int i = 0;
		}

		protected override void OnDispose() {
			foreach (ViewModel viewmodel in Children) {
				viewmodel.Dispose();
			}
			this.Children.Clear();
			this.Children.CollectionChanged -= children_CollectionChanged;
		}
	}
}